<?
use adminmatrix\migration\Migrator;
use  adminmatrix\migration\db\Column;

class Admin extends Migrator
{
    /**
         * Change Method.
         *
         * Write your reversible migrations using this method.
         *
         * More information on writing migrations is available here:
         * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
         *
         * The following commands can be used in this method and Phinx will
         * automatically reverse them when rolling back:
         *
         *    createTable
         *    renameTable
         *    addColumn
         *    renameColumn
         *    addIndex
         *    addForeignKey
         *
         * Remember to call "create()" or "update()" and NOT "save()" when working
         * with the Table class.
     */
    public function up()
    {
        $this->createAdminUserTable();
        $this->createAdminInfoTable();
        $this->createAdminSession();
    }

    public function down() {
        $this->table('admin_user')->drop()->save();
        $this->table('admin_info')->drop()->save();
        $this->table('admin_session')->drop()->save();
    }

    private function createAdminUserTable()
    {
        $table = $this->table('admin_user', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '用户表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('username', 'string', [
                'null' => false,
                'limit' => 50,
                'comment' => '用户名'
            ])
            ->addColumn('password', 'string', [
                'null' => false,
                'limit' => 255,
                'comment' => '密码'
            ])
            ->addColumn('salt', 'string', [
                'null' => true,
                'limit' => 64,
                'comment' => '密码盐'
            ])
            ->addColumn('nickname', 'string', [
                'null' => true,
                'limit' => 64,
                'comment' => '昵称'
            ])
            ->addColumn('last_login_time', 'integer', [
                'null' => true,
                'limit' => 11,
                'comment' => '最后登录时间'
            ])
            ->addColumn('last_login_ip', 'string', [
                'null' => true,
                'limit' => 32,
                'comment' => '最后登录ip'
            ])

            ->addColumn('root', 'tinyinteger', [
                'null' => true,
                'limit' => 1,
                'default'=>0,
                'comment' => '是否为超级管理员'
            ])

            ->addColumn('avatar', 'string', [
                'null' => true,
                'limit' => 11,
                'comment' => '头像'
            ])->addColumn('status', 'tinyinteger', [
                'null' => true,
                'limit' => 1,
                'default'=>1,
                'comment' => '状态 1：开启 0 关闭'
            ])

            ->addColumn('multipoint_login' , 'tinyinteger' , [
                'null'  =>  true,
                'limit' =>  1,
                'default'   =>  1,
                'comment'    => '是否支持多处登录：1-是；0-否；'
            ])

            ->addColumn('create_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '创建时间'
            ])->addColumn('update_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '更新时间'
            ])->addColumn('delete_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '删除时间'
            ]);

        $table->addIndex('username', [
            'name' => 'username',
            'unique' => true
        ]);
        $table->create();

        if ($this->hasTable('admin_user') && $this->isMigratingUp())
        {
            $salt = uniqid('matrix_');
            # 插入数据
            $this->table('admin_user')->insert([
                [
                    'username'     => 'admin',
                    'password'     =>  md5('admin' . $salt),
                    'salt'         =>  $salt,
                    'root'          => 1,
                    'create_time'   =>  time(),
                ]
            ])->save();
        }
    }

    private function createAdminInfoTable() {
        $table = $this->table('admin_info', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '管理员信息表',
            'id' => false,
        ]);

        $table->addColumn('admin_id', 'integer', [
            'null' => false,
            'comment' => '用户id'
        ]);
        $table->addColumn('organization_id', 'integer', [
            'null' => true,
            'comment' => '组织 ID'
        ]);
        $table->addColumn('department_id', 'integer', [
            'null' => true,
            'comment' => '部门 ID'
        ]);
        $table->addColumn('position_id', 'integer', [
            'null' => true,
            'comment' => '职位 ID'
        ]);

        $table->addIndex('admin_id', [
            'name' => 'admin_id',
            'unique'    =>  false,
        ]);

        $table->create();
    }

    private function createAdminSession(){
        $table  = $this->table('admin_session' , [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'id' => 'id',
            'primary_key' => ['id'],
            'comment' => '管理员会话表',
        ]);

        $table
            ->addColumn('admin_id' , 'integer' , [
                'null'  =>  false,
                'comment'   =>  '用户id'
            ])

            ->addColumn('terminal', 'tinyinteger', [
                'null' => true,
                'limit' => 11,
                'comment' => '客户端类型：1-pc管理后台 2-mobile手机管理后台'
            ])

            ->addColumn('token' , 'string' , [
                'null'  =>  false,
                'limit'     =>  32,
                'comment'   =>  '令牌',
                'collation'   =>  'utf8mb4_general_ci',
            ])->addColumn('expire_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '到期时间'
            ])->addColumn('update_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '更新时间'
            ])->addColumn('delete_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '删除时间'
            ]);

        $table->addIndex(['admin_id', 'terminal'], [
            'name' => 'admin_id_client',
            'unique' => true,
        ]);

        $table->addIndex('token', [
            'name' => 'token',
            'unique' => true,
        ]);

        $table->create();

    }

}